﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using CommunityServer.WebServices.MediaGalleries;
using System.Web;
using CommunityServer.WebServices.Common;

namespace nb.development.csmediamanager
{
    public class PostWorker
    {
        public PostWorker(string path, Site site)
        {
            this.site = site;
            this.path = path;
        }

        private Site site;
        private string path;
        public event PostWorkerCompleted Completed;
        public event PostAdded PostAdded;

        public void Run()
        {
            try
            {
                MediaGalleriesService mgs = site.Service;
                string[] paths = path.Split(new char[] { '\\' }, StringSplitOptions.RemoveEmptyEntries);
                if (paths.Length == 3)
                {
                    int id = int.Parse(paths[2]);
                    PagedList<MediaGalleryPost> posts = mgs.GetMediaGalleryPosts(id);
                    foreach (MediaGalleryPost post in posts)
                        PostAdded(post, path);
                    if (posts.Count < posts.TotalCount)
                    {
                        for (int x = 0; x <(posts.TotalCount / posts.Count); x++)
                        {
                            try
                            {
                                foreach (MediaGalleryPost post in mgs.GetMediaGalleryPosts(id, x, posts.Count))
                                    PostAdded(post, path);
                            }
                            catch { }
                        }
                    }
 
                }
                else
                {
                    MediaGalleryPostQuery query = new MediaGalleryPostQuery();
                    query.MediaGalleryIds.Add(int.Parse(paths[2]));
                    query.Keywords.Add(HttpUtility.HtmlEncode(paths[3]));
                    query.PageIndex = 1;
                    query.PageSize = 2;
                    PagedList<MediaGalleryPost> posts = mgs.GetMediaGalleryPosts(query);
                    foreach (MediaGalleryPost post in posts)
                        PostAdded(post, path);
                    if (posts.Count < posts.TotalCount)
                    {
                        for (int x = 0; x < (posts.TotalCount / posts.Count); x++)
                        {
                            try
                            {
                                query.PageIndex++;
                                foreach (MediaGalleryPost post in mgs.GetMediaGalleryPosts(query))
                                    PostAdded(post, path);
                            }
                            catch { }
                        }
                    }
                }
                if (Completed != null)
                    Completed();
            }
            catch (Exception ex) { MessageBox.Show("Error:\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        }
    }

    public delegate void PostAdded(MediaGalleryPost post, string path);
    public delegate void PostWorkerCompleted();
}
